package com.hsgc.servce;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

import com.hsgc.dao.ProductDao;
import com.hsgc.info.PageInfo;
import com.hsgc.model.Product;
import com.hsgc.model.ProductQuery;
import com.hsgc.threadlocal.LocalPageInfoUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class ProductService {
    @Resource
    private ProductDao productDao;

    public List<Product> findAll(){

        return productDao.findAll();
    }
    //根据条件查询和全部查询
    public List<Product> select(ProductQuery query){
        if (query.getPageNum()!=null && query.getPageSize()!=null){
        PageHelper.startPage(query.getPageNum(),query.getPageSize());
        }
        List<Product> select = productDao.select(query);
        //判断你是不是page这个类 如果是这个类就强转一下
        if(select instanceof Page){
            Page page= (Page) select;
            long total = page.getTotal();
            System.out.println(total);
            LocalPageInfoUtil.set(PageInfo.builder().Total(total).build());
        }
        return select;
    }

    public Integer insert(Product category){
        return productDao.insert(category);
    }
    public Integer update(Product category){
        return  productDao.update(category);
    }
    //软删除  只是修改了以下状态信息
    public Integer updateStatus(int id){
        return productDao.updateStatus(id,0);
    }

}
